<?php

namespace app\admin\model;

use think\Model;

class Setmeal extends Model
{


    protected $insert = ['status' => '0'];

    // 表名
    protected $name = 'setmeal';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];

    // 字段类型或者格式转换
    protected $type = [
        'create_time' => 'datetime:Y-m-d H:i:s',
        'update_time' => 'datetime:Y-m-d H:i:s'
    ];


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }

    public function category()
    {
        return $this->belongsTo('app\common\model\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function dishes()
    {
        return $this->belongsToMany('Dish', 'setmeal_dish')->withPivot('copies');
    }
}
